package com.ro.model.blog;

import com.jfinal.aop.Before;
import com.jfinal.aop.Clear;
import com.ro.common.AbstractController;
import com.ro.common.model.Blog;

@Before({BlogInterceptor.class, AuthInterceptor.class})
public class BlogController extends AbstractController {

    static BlogService service = new BlogService();

    @Clear
    @Before(AuthStateInterceptor.class)
    public void index() {
        setAttr("blogPage", service.paginate(getParaToInt(0, 1), 10, getPara("searchStr", "")));
        render("blog.html");
    }


    public void add() {
    }

    @Before(BlogValidator.class)
    public void save() {
        Blog blog = getBean(Blog.class);
        service.createBlog(blog);
        redirect("/blog/manage");
    }

    public void edit() {
        setAttr("blog", service.getBlogEc(getParaToInt()));
    }

    @Before(BlogValidator.class)
    public void update() {
        Blog blog = getBean(Blog.class);
        service.updateBlog(blog);
        redirect("/blog/manage");
    }

    public void delete() {
        service.deleteById(getParaToInt());
        redirect("/blog/manage");
    }

    @Clear
    @Before(AuthStateInterceptor.class)
    public void detail() {
        Blog blog = service.getBlogEc(getParaToInt());
        setSeo(this, blog.getTitle(), blog.getKeyWord(), blog.getDescribe());
        service.updateAddReadNum(blog.getId());
        setAttr("blog", blog);
    }

    public void manage() {
        setAttr("blogPage", service.paginateManage(getParaToInt(0, 1), 30, getPara("searchStr", "")));
        render("manage.html");
    }

    public void refreshCache() {
        service.refreshCache();
        redirect("/blog/manage");
    }

}


